using System;
using System.Text;
using System.IO;
using System.Collections;
   
namespace Calendar {
    public class VCard       {
        private string _formattedName;
        private string _lastName;
        private string _firstName;
        private string _middleName;
        private string _namePrefix;
        private string _nameSuffix;
        private DateTime _birthDate = DateTime.MinValue;
        private ArrayList _addresses = new ArrayList();
        private ArrayList _phoneNumber = new ArrayList();
        private ArrayList _emailAddresses = new ArrayList();
        private string _title;
        private string _role;
        private string _organization;
           
        public VCard() {
            //
            // TODO: Add constructor logic here
            //
        }
  
        #region Public Properties
        public string FormattedName {
            get{return _formattedName;}
            set{_formattedName = value;}
        }
        public string LastName {
            get{return _lastName;}
            set{_lastName = value;}
        }
        public string FirstName {
            get{return _firstName;}
            set{_firstName = value;}
        }
        public string MiddleName {
            get{return _middleName;}
            set{_middleName = value;}
        }
        public string NamePrefix {
            get{return _namePrefix;}
            set{_namePrefix = value;}
        }
        public string NameSuffix {
            get{return _nameSuffix;}
            set{_nameSuffix = value;}
        }
        public DateTime BirthDate {
            get{return _birthDate;}
            set{_birthDate = value;}
        }
        public ArrayList Addresses {
            get{return _addresses;}
            set{_addresses = value;}
        }
        public ArrayList PhoneNumber {
            get{return _phoneNumber;}
            set{_phoneNumber = value;}
        }
        public ArrayList EmailAddresses {
            get{return _emailAddresses;}
            set{_emailAddresses = value;}
        }
        public string Title {
            get{return _title;}
            set{_title = value;}
        }
        public string Role {
            get{return _role;}
            set{_role = value;}
        }
        public string Organization {
            get{return _organization;}
            set{_organization = value;}
        }
 
        #endregion
 
        #region Public Methods
 
        public void Generate(string filePath, FileMode mode) {
            FileStream fs = new FileStream(filePath, mode);
 
            Generate(fs);
        }
 
        public void Generate(Stream outputStream) {
            StreamWriter sw = new StreamWriter(outputStream, System.Text.Encoding.Default);
 
            using (sw) {
                sw.Write(this.ToString());
            }
        }
        #endregion
 
        public override string ToString() {
            StringBuilder sb = new StringBuilder();
 
            //Start VCard
            sb.Append("BEGIN:VCARD\r\n");
 
            //Add Formatted name
            if( _formattedName != null ) {
                sb.Append("FN:" + _formattedName + "\r\n");
            }
 
            //Add the name
            sb.Append("N:" + _lastName + ";");
            sb.Append(_firstName + ";");
            sb.Append(_middleName + ";");
            sb.Append(_namePrefix + ";");
            sb.Append(_nameSuffix + "\r\n");
 
            //Add a birthday
            if( _birthDate != DateTime.MinValue ) {
                sb.Append("BDAY:" + _birthDate.ToString("yyyyMMdd") + "\r\n");
            }
 
            //Add Delivery Addresses
            foreach(DeliveryAddress da in _addresses) {
                sb.Append(da.ToString());
            }
 
            //Add phone numbers
            foreach(TelephoneNumber phone in _phoneNumber) {
                sb.Append(phone.ToString());
            }
 
            //Add email address
            foreach(string email in _emailAddresses) {
                sb.Append("EMAIL; INTERNET:" + email + "\r\n");
            }
 
            //Add Title
            sb.Append("TITLE:" + _title + "\r\n");
 
            //Business Category
            sb.Append("ROLE:" + _role + "\r\n");
 
            //Organization
            sb.Append("ORG:" + _organization + "\r\n");
 
            sb.Append("END:VCARD\r\n");
 
            return sb.ToString();
        }
    }
}